﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Daycare.DAL;
using Daycare.Models;
using ImageResizer;

namespace Daycare.Services
{
    public class ImageService
    {
        private ImageRepository ir;

        public ImageService()
        {
            ir = new ImageRepository();
        }

        public Daycare.Models.Image AddImage(String fileName)
        {
            return ir.AddImage(fileName);
        }

        public String UploadImage(String uploadedFileName, String userName)
        {
            String uniqueFileName = String.Empty;

            if (HttpContext.Current.Request.Files.Keys.Count > 0)
            {
                HttpPostedFile file = HttpContext.Current.Request.Files[0];
                if (file.ContentLength > 0)
                {
                    uniqueFileName = (userName + DateTime.Now.ToFileTime() + ".png");

                    ImageJob image = new ImageJob(file, String.Format("~/{0}{1}", Configuration.ImagePath, uniqueFileName), new ImageResizer.ResizeSettings("width=250;height=250;format=png;scale=both"));
                    ImageJob thumbnail = new ImageJob(file, String.Format("~/{0}t_{1}", Configuration.ImagePath, uniqueFileName), new ImageResizer.ResizeSettings("width=75;height=75;format=png;scale=both"));
                    image.Build();
                    thumbnail.Build();
                }
            }

            return uniqueFileName;
        }
    }
}